package com.bootdo.system.controller;

import com.bootdo.system.domain.BizhongDO;
import com.bootdo.system.domain.FrontuserDO;
import com.bootdo.system.domain.JiaoyiVO;
import com.bootdo.system.domain.JiaoyicelueDO;
import com.bootdo.system.service.BizhongService;
import com.bootdo.system.service.FrontuserService;
import com.bootdo.system.service.JiaoyicelueService;
import com.bootdo.system.utils.R;
import com.huobi.HuobiInfoUtils;
import com.huobi.exception.SDKException;
import com.huobi.model.account.Balance;
import com.huobi.model.market.MarketTicker;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.IncorrectUpdateSemanticsDataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.java2d.opengl.OGLContext;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.net.SocketTimeoutException;
import java.util.ArrayList;
import java.util.List;

@ResponseBody
@Controller
public class JiaoYiController extends BaseController {

    @Autowired
    private JiaoyicelueService jiaoyicelueService;
    @Autowired
    private FrontuserService frontuserService;
    @Autowired
    private BizhongService bizhongService;

    @GetMapping("/alljiaoyishezhi")
    public String alljiaoyishezhi(Integer[] bizhongids, Model model) {

        String str = "";

        for (Integer bizhongid : bizhongids) {

            str = str + bizhongid + "-";

        }

        model.addAttribute("bizhongids", str);

        return "celueedit2";
    }

    @GetMapping("/jiaoyi/detail")
    public R detail(Long id, HttpServletRequest request) {
        //交易策略
        JiaoyicelueDO jiaoyicelueDO = jiaoyicelueService.get(id);

        //币种名字
        BizhongDO bizhongDO = bizhongService.get(jiaoyicelueDO.getBizhongid());

        if (jiaoyicelueDO.getChicangjunjia().compareTo(BigDecimal.ZERO) > 0) {
            BigDecimal subtract = bizhongDO.getNowprice().subtract(jiaoyicelueDO.getChicangjunjia());
            BigDecimal divide = subtract.divide(jiaoyicelueDO.getChicangjunjia(), 4, BigDecimal.ROUND_UP);
            jiaoyicelueDO.setZhangfu(divide);
            jiaoyicelueService.update(jiaoyicelueDO);
        }

        return R.ok()
                .put("bizhongDO",bizhongDO)
                .put("jiaoyicelueDO",jiaoyicelueDO);
    }

    @GetMapping("/jiaoyi")
    public R jiaoyiPage(String keyword,HttpServletRequest request) {

        System.out.println("关键字="+keyword);

        //全部循环
        List<JiaoyiVO> jiaoyiVOS = jiaoyicelueService.listwithKeyword(getUserId(request), keyword);
        //策略循环
        List<JiaoyiVO> celuelist = new ArrayList<>();
        //单次循环
        List<JiaoyiVO> dancilist = new ArrayList<>();
        //停止
        List<JiaoyiVO> tingzhilist = new ArrayList<>();

        for (JiaoyiVO jiaoyi : jiaoyiVOS) {
            //循环的开启的
            if (jiaoyi.getXunhuantype() == 1&&jiaoyi.getCeluestatus()==1) {
                celuelist.add(jiaoyi);
            }
            //单次的开启的
            if(jiaoyi.getXunhuantype() == 0&&jiaoyi.getCeluestatus()==1) {
                dancilist.add(jiaoyi);
            }
            //停止的
            if (jiaoyi.getCeluestatus() == 0) {
                tingzhilist.add(jiaoyi);
            }
        }

        return R.ok()
                .put("jiaoyiVOS",jiaoyiVOS)
                .put("celuelist",celuelist)
                .put("dancilist",dancilist)
                .put("tingzhilist",tingzhilist);

    }

    @GetMapping("/getaccount")
    public R getaccount(HttpServletRequest request) {
        String account = "0.00";
        FrontuserDO userDO = getUserDO(request);
        try {
            if (StringUtils.isNotEmpty(userDO.getHuobiapikey()) && StringUtils.isNotEmpty(userDO.getHuobiapisecret())) {
                Balance accountyue = HuobiInfoUtils.getAccountyue(userDO.getHuobiapikey(), userDO.getHuobiapisecret());
                account = accountyue.getBalance().stripTrailingZeros().toPlainString();
            }
        } catch (SDKException e) {
            System.out.println("交易页面数据获取失败：" + e);
            account = "0.00";
        }

        return R.ok().put("account", account);
    }
}
